﻿using System;
using System.Linq;
using System.Threading.Tasks;
namespace Athena.RemoteAddress.Implementations
{
    public abstract class BaseAddressModelSelector : IAddressModelSelector
    {
        Task<RemoteAddressModel> IAddressModelSelector.SelectAsync(AddressModelSelectContext addressModelSelectContext)
        {
            if (addressModelSelectContext == null)
            {
                throw new ArgumentNullException(nameof(addressModelSelectContext));
            }
            if (addressModelSelectContext.AddressModelDescriptor == null)
            {
                throw new ArgumentNullException(nameof(addressModelSelectContext.AddressModelDescriptor));
            }
            if (string.IsNullOrWhiteSpace(addressModelSelectContext.AddressModelDescriptor.CorrelationId))
            {
                throw new ArgumentNullException(addressModelSelectContext.AddressModelDescriptor.CorrelationId);
            }
            return addressModelSelectContext.AddressModelDescriptor.RemoteAddress.Count() == 1 ? Task.FromResult(addressModelSelectContext.AddressModelDescriptor.RemoteAddress.First()) : SelectAsync(addressModelSelectContext);
        }

        protected abstract Task<RemoteAddressModel> SelectAsync(AddressModelSelectContext addressModelSelectContext);
    }


}
